<template>
  <div class="dashboard-container">
    <aside class="aside-style">
      <el-row>
        <el-col :span="9">
          <el-date-picker
            v-model="datetime"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
            :default-time="['00:00:00', '23:59:59']"
            value-format="yyyy-MM-dd"
            clearable
            @change="dateChange"
          />
        </el-col>
        <el-col :span="5">
          <el-select v-model="department_id" clearable placeholder="请选择部门" @change="departmentChange">
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.department"
              :value="item.id"
            />
          </el-select>
        </el-col>
        <el-col :span="5">
          <el-input
            v-model="user_name"
            placeholder="请输入用户名"
            clearable
          >
            <el-button slot="append" @click="userNameChange">查询</el-button>
          </el-input>
        </el-col>
      </el-row>
    </aside>
    <el-table
      :data="list"
      border
      style="width: 100%;margin-top:20px"
    >
      <el-table-column
        prop="user_name"
        label="姓名"
        width="180"
      />
      <el-table-column
        prop="department"
        label="部门"
        width="180"
      />
      <el-table-column
        label="状态"
      >
        <template slot-scope="scope">
          {{ scope.row.status === '401' ? '拒绝' : '删除' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="创建时间"
      />
      <el-table-column
        label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="uploadStatusFun(scope.row)">恢复或通过</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" @pagination="paginationFun" />
    <department-info ref="departmentInfo" @reloadFun="reloadFun" />
  </div>
</template>

<script>
import { getUserListApi, getDepartmentListApi, checktUserApi } from '@/api/main'
import pagination from '@/components/Pagination'
import departmentInfo from './components/user-department'
export default {
  components: {
    pagination,
    departmentInfo
  },
  data() {
    return {
      list: [],
      departmentList: [],
      p: 1,
      limit: 10,
      total: 0,
      datetime: '',
      start_time: '',
      end_time: '',
      user_name: '',
      department_id: '',
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
    this.getUserList()
    this.getDepartmentList()
  },
  methods: {
    paginationFun(val) {
      this.p = val.page
      this.limit = val.limit
      this.getUserList()
    },
    uploadStatusFun(row) {
      this.$confirm('此操作将恢复用户' + row.user_name + '为正常用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          user_id: row.id,
          status: 200
        }
        checktUserApi(data).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.getUserList()
          }
        })
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // })
      })
    },
    reloadFun() {
      this.getUserList()
    },
    userNameChange() {
      this.getUserList()
    },
    getDepartmentList() {
      getDepartmentListApi().then(res => {
        if (res.code === 200) {
          this.departmentList = res.list
        }
      })
    },
    departmentChange(val) {
      this.department_id = val
      console.log(val)
      this.getUserList()
    },
    dateChange(val) {
      if (val) {
        this.start_time = val[0] + ' 00:00:00'
        this.end_time = val[1] + ' 23:59:59'
      } else {
        this.start_time = ''
        this.end_time = ''
      }
      this.getUserList()
    },
    getUserList() {
      const data = {
        status: 4,
        p: this.p,
        limit: this.limit,
        start_time: this.start_time,
        end_time: this.end_time,
        user_name: this.user_name,
        department_id: this.department_id
      }
      console.log(data)
      getUserListApi(data).then(res => {
        this.list = res.list
        this.total = Number(res.count)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
    &-container {
        margin: 30px;
    }
    &-text {
        font-size: 30px;
        line-height: 46px;
    }
}
</style>
